﻿@{
    ViewBag.Title = "AdvertiseSet";
}

@section scripts
{  
    <link rel="stylesheet" href="../Scripts/kindeditor-4.1.10/themes/default/default.css" />
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/kindeditor-min.js"></script>
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/lang/zh_CN.js"></script>    
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
}
    <form id="formt" runat="server" method="post" style="height: 100%">
    
        <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
            <table width="1000px;">
                <tr>
                    <td width="1000px;">
                        <a class="mini-button" id="save" name="save" iconcls="icon-save" onclick="SaveInfo" plain="true">保存</a>
                        <span class="separator"></span>
                        <a class="mini-button" id="close" iconcls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>
        </div>

        <table style="margin-top:15px;">
            <tr>
                <td width="70px" align="right">广告编码：</td>
                <td width="190px" align="left">
                    <input name="FNumber" class="mini-textbox" style="width: 190px;" required="true" TabIndex="1" />
                </td>
                <td width="70px" align="right">广告类型：</td>
                <td width="190px" align="left">
                    <input name="comdatatype" class="mini-combobox" style="width: 190px;" value="0"
                           shownullitem="false" required="true" allowinput="false" align="left" data="mdatatype" TabIndex="4" />
                </td>
                <td width="70px" align="right">图片URL：</td>
                <td width="190px" align="left">
                    <input name="FImgUrl" class="mini-textbox" style="width: 190px;" />
                </td>
            </tr>
            <tr>
                <td width="70px" align="right">广告名称：</td>
                <td width="190px" align="left">
                    <input name="FName" class="mini-textbox" style="width: 190px;" required="true" TabIndex="2" />
                </td>
                <td width="70px" align="right">排序：</td>
                <td width="190px" align="left">
                    <input name="FOrder" class="mini-spinner" style="width:190px;" minValue="0" maxValue="100" showbutton="false" TabIndex="5" />
                </td>
                <td></td>
                <td rowspan="4"><img id="Image0" src="~/Content/Images/NoImage.jpg" Width="190" height="120" /></td>
            </tr>
            <tr>
                <td width="70px" align="right">导航类型：</td>
                <td width="190px" align="left">
                    <input name="comUrlType" class="mini-combobox" style="width: 190px;" value="0"
                           shownullitem="false" required="true" allowinput="false" align="left" data="mUrlType" TabIndex="4" />                    
                </td>
                <td width="70px" align="right">导航位置：</td>
                <td width="190px" align="left">
                    <input id="FLinkUrl" name="FLinkUrl" class="mini-buttonedit" onbuttonclick="getClassItem('')" style="width: 190px;" TabIndex="6" />
                </td>
                <td></td>
            </tr>
            <tr>
                <td width="70px" align="right">备注：</td>
                <td width="190px" align="left">
                    <input name="FRemark" class="mini-textbox" style="width: 190px;" TabIndex="3" />
                </td>
                <td width="70px" align="right"></td>
                <td width="190px" align="left">
                </td>
                <td></td>
            </tr>
            <tr style="height:30px;">
                <td></td>
                <td align="left">
                    <a name="FSendFlag" class="mini-checkbox" style="width: 100px; margin-left: 10px;">是否启用</a>
                </td>
                <td></td>
                <td align="left">
                    <!--
            <a name="SaveNew" class="mini-checkbox" style="width: 100px; margin-left: 10px;">连续新增</a>
            &nbsp;-->
                </td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td colspan="2">
                    图像建议尺寸：300*100px<br />
                    轮播广告尺寸：375*175px
                </td>
            </tr>
        </table>
                <table>
                    <tr>
                    </tr>
                </table>

                <table>
                    <tr>
                        <td width="470px" align="left">
                            <div class="rightinfo">
                                <div class="tools">
                                </div>
                                <div id="div_imglist">
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
                <table>
                    <tr>
                    </tr>
                </table>
            <input id="ImagFile0" name="ImagFile0" class="mini-hidden" />  
        <input id="ImageFileUrl" type="hidden" />
            <input name="FItemID" class="mini-hidden" />
            <input name="FDataType" class="mini-hidden" />
            <input name="FGoodsClsID" class="mini-hidden" />
            <input name="FGoodsID" class="mini-hidden" />
@*            <div style="display:none;">
                <textarea id="contentHtml" cols="0" rows="0" style="width:0px;height:0px;"></textarea>
            </div>*@
      </form>

<script>
    var editor1;

    var mdatatype = [{ "id": "0", "text": "轮播广告" },
                     { "id": "1", "text": "分类导航" },
        { "id": "2", "text": "广告位广告(公众号)" },
        { "id": "3", "text": "分类精选" }];

    var mUrlType = [{ "id": "0", "text": "导航到商品详情" },
                     { "id": "1", "text": "导航到商品分类" }];

    function FormShown() {
        mini.parse();
        loaded();
        $(".ke-container").css("widht", "0px");
        $(".ke-container").css("height", "0px");
    }

    //加载资料
    function SetInfoData(data) {
        if (data.action == "new") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            NewInfo();
        }
        else {
            $.post("GetAdvertInfo", { mjson: "[{'FItemID':'" + data.FItemID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
                var form = new mini.Form("formt");
                var mdata = mini.decode(data);
                mini.getbyName("FItemID").setValue(mdata.data[0].FItemID);
                form.setData(mdata.data[0]);
                mini.getbyName("FImgUrl").setValue(mdata.data[0].FImgUrl);
                mini.getbyName("comdatatype").setValue(mdata.data[0].FDataType);
                mini.getbyName("comUrlType").setValue(mdata.data[0].FUrlType);
                if (mdata.data[0].FImgUrl == "")
                    $("#Image0").attr('src', "/Content/Images/NoImage.jpg");
                else
                    $("#Image0").attr('src', ImageFilePath(mdata.data[0].FImgUrl));
                
                if ((mdata.data[0].FUrlType == 0) && (mdata.data[0].FGoodsID > 0)) {
                    mini.getbyName("FLinkUrl").setText(mdata.data[0].FGoodsName);
                }
                else {
                    mini.getbyName("FLinkUrl").setText(mdata.data[0].FGoodsClsName);
                }
            });
        }
    }

    //新增资料
    function NewInfo() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("FNumber").setValue("");
        mini.getbyName("FName").setValue("");
        mini.getbyName("comdatatype").setValue("0");
        mini.getbyName("FRemark").setValue("");
        mini.getbyName("FLinkUrl").setValue("");
        mini.getbyName("FImgUrl").setValue("");
        mini.getbyName("FGoodsClsID").setValue(0);
        mini.getbyName("FGoodsID").setValue(0);
    }

    //保存资料
    function SaveInfo() {
        if (mini.getbyName("FLinkUrl").getValue() == '') {
            mini.alert("没有选择导航位置，不能保存。");
            return;
        }        

        mini.getbyName("save").disable();
        var form = new mini.Form("#formt");
        form.validate();
        if (form.isValid() == false) {
            mini.getbyName("save").enable();
            return;
        }
        var data = form.getData(true, false);
        data.mSystemAppID = parent.window.getSystemAppID();
        var json = mini.encode(data);
        $.post("SaveAdvertise", { mjson: json}, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("save").enable();
        });
    }

    function getClassItem(mSearchKey) {

        var sUrlType = mini.getbyName("comUrlType").getValue();

        if (sUrlType == 0) {
            mini.open({
                url: "../Bill/GetBillGoods",
                title: "选择商品", width: 900, height: 500,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { key: '', mBillType: '' };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    var ID = ""; var namestr = "";
                    if (action.length>0) {
                        ID = action[0].FItemID;
                        namestr = action[0].FName;
                        var data = '{mSystemAppID:"' + parent.window.getSystemAppID() + '",FItemID:"' + ID + '",BID:"-1"}';
                        $.post("geturl", { json: data }, function (data) {
                            mini.getbyName("FLinkUrl").setValue(data);
                        });
                        mini.getbyName("FLinkUrl").setText(namestr);
                        mini.getbyName("FGoodsClsID").setValue(0);
                        mini.getbyName("FGoodsID").setValue(ID);
                    }
                }
            });
        }
        else {
            mini.open({
                url: "../GetItem/index",
                title: "商品分类选择", width: 730, height: 460,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "new", sTbName: "GoodsClsInfo", sSearchKey: mSearchKey };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    var ID = ""; var namestr = "";
                    if (action.length>0) {
                        ID = action[0].FItemID;
                        namestr = action[0].FName;
                        var data = '{mSystemAppID:"' + parent.window.getSystemAppID() + '",FItemID:"' + ID + '",BID:"0"}';
                        $.post("geturl", { json: data }, function (data) {
                            mini.getbyName("FLinkUrl").setValue(data);
                        }); 
                    }
                    mini.getbyName("FLinkUrl").setText(namestr);
                    mini.getbyName("FGoodsClsID").setValue(ID);
                    mini.getbyName("FGoodsID").setValue(0);
                }
            });
        }
    }
    function ImageFilePath(url) {
        var result = "";
        $.ajax({
            url: "/GetItem/GetImageFilePath",
            data: { Url: url },
            type: "post",
            async: false,
            success: function (data) {
                result = data;
            },
            failure: function () {
                result = "";
            },
            error: function (jqXHR, textStatus, errorThrown) {
                return "";
            }
        });
        return result;
    }

    function loaded() {
        KindEditor.ready(function (K) {
            var editor1 = K.editor({
                uploadJson: '../asp.net/upload_json.ashx?dirs=Mall&mSystemAppID=' + parent.window.getSystemAppID() + '&thumb=true',
                fileManagerJson: '../asp.net/file_manager_json.ashx?dirs=Mall&mSystemAppID=' + parent.window.getSystemAppID(),
                allowFileManager: true
            });

            K('#Image0').click(function () {
                var url = $(this).attr('src');
                if (url.toLowerCase() != "/content/images/noimage.jpg") {
                    var cfg = confirm("你确定要删除当前图片信息？");
                    if (!cfg) {
                        return;
                    }
                    else {
                        $(this).attr('src', "/content/Images/NoImage.jpg");
                        mini.getbyName("ImagFile0").setValue("");
                        return;
                    }
                }

                editor1.loadPlugin('image', function () {
                    editor1.plugin.imageDialog({
                        imageUrl: K('#ImageFileUrl').val(),
                        clickFn: function (url, title, width, height, border, align) {
                            var path = ImageFilePath(url);
                            $("#Image0").attr('src', path);
                            $("#ImagFile0").val(path);
                            mini.getbyName("FImgUrl").setValue(url);
                            editor1.hideDialog();
                        }
                    });
                });
            });

        });
    }


    function ImageFilePath(url) {
        var result = "";
        $.ajax({
            url: "/GetItem/GetImageFilePath",
            data: { Url: url },
            type: "post",
            async: false,
            success: function (data) {
                result = data;
            },
            failure: function () {
                result = "";
            },
            error: function (jqXHR, textStatus, errorThrown) {
                return "";
            }
        });
        return result;
    }

</script>
